var form = layui.form,id=getQueryVariable("id"),laydate = layui.laydate;

//表单初始值，修改时调用
if(id!=""){

   // $('#newcustomer-form').find('input,textarea,select').attr('disabled',true);
   //  $("input[name='name']").attr("readOnly",true);
    queryByid(id);
}
laydate.render({
    elem: '#customerbirthday'
});
//监听提交
form.on('submit(newcustomer-form-element)', function(data){
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    if(insert(data)){
        layer.msg("保存成功", {icon: 6});
        refrash();
    }else{
        layer.msg("保存失败", {icon: 6});
    }

    return false;
});
//刷新
function refrash() {
    setTimeout(function () {
        parent.table.reload('customertableid', {
            type: 'get'
            ,url:"customercontact/selectByPage"
            ,where:{
                "company":parent.customercode
            }
            ,   page: {
                curr: 1 //重新从第 1 页开始
            }


        }, 'data'); //刷新联系人表格
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);

    },1000);
}
function queryByid(da) {
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTre,
        data:{"id":da},
        url:'customercontact/queryByid',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
                init(da.data[0]);
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
}
function  init(data) {
    form.val("newcustomer-form",{
        "name":data.name,
        "email":data.email,
        "post":data.post,
        "tel":data.tel,
        "phone":data.phone,
        "wx":data.wx,
        "birthday":data.birthday,
        "expressAddress":data.expressAddress,
        "remark":data.remark

    })
    form.render();
        /*,{field:'name', title:'联系人名称', sort: true}
        ,{field:'email', title:'邮箱地址', sort: true}
        ,{field:'post', title:'职务', sort: true}
        ,{field:'tel', title:'电话', sort: true}
        ,{field:'phone', title:'手机', sort: true}
        ,{field:'wx', title:'微信', sort: true}
        ,{field:'expressAddress', title:'快件地址', sort: true}
        ,{field:'birthday', title:'联系人生日', sort: true}
        ,{field:'remark', title:'联系人备注', sort: true}*/
}
//保存
function insert(data) {
    // let reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //正则表达式
    // let emailValue=$("#email").val();
    // if(emailValue!="" && !reg.test(emailValue)){ //正则验证不通过，格式不正确
    //     layer.msg("验证不通过!", {icon: 5});
    //     return false;
    // }
    data.field.id=id;
    data.field.company=parent.customercode;
    var tf=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data.field),
        url:'customercontact/insert',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                tf=true;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return tf;
}
$("#email").blur(function (data) {
    if(data.target.value==""){
       return false;
    }
    if(data.target.name=="email"){//简称
        $.ajax({
            type : 'get',
            //  url : 'resource/getTree',
            data:{"zd":data.target.name,"text":data.target.value,"id":id+"#"+parent.salmanValue[0]},
            url:'customer/isExist',
            dataType:"json",
            /*  contentType: 'application/json',*/
            async:false,
            success : function (da) {
                if(da.status==200 ){
                    if(da.data !=""){

                        layer.msg(da.data);
                       $("#email").val("");
                    }
                }
            },
            error: function(data) {
                // layer.msg("获取失败!");
            }
        });
    }
    return false;
});